package com.yonyou.iuap.shell.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.yonyou.iuap.shell.dao.BdIndustryChartEntityDao;
import com.yonyou.iuap.shell.entity.BdIndustryChartEntity;
import com.yonyou.iuap.shell.entity.exp.BdHyGxSbBasePoJo;
import com.yonyou.iuap.shell.entity.exp.BdHyGxSbPoJo;
import com.yonyou.iuap.shell.entity.exp.BdIndustryTreePoJo;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IIndustryChartService;
import com.yonyou.iuap.shell.util.MathUtil3;

@Service("service-IndustryChart")
public class IndustryChartServiceImpl implements IIndustryChartService{

	@Autowired
	@Qualifier("Dao-BdIndustryChartEntityDao")
	private BdIndustryChartEntityDao<BdIndustryChartEntity>  bdIndustryChartEntityDao;
	
	public ResultDto deleteByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i_del = bdIndustryChartEntityDao.deleteByPrimaryKey(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		
		return rsDto;
	}

	public ResultDto insert(BdIndustryChartEntity supplier) {
		ResultDto rsDto = new ResultDto();
        if (supplier == null) {
            rsDto.setStatus(1);
            rsDto.setMessage("添加失败！");
            return rsDto;
        }
        supplier.setPk_industry_chart(MathUtil3.uuid32());

        //添加cate_code
        StringBuffer aa = new StringBuffer();
        if (supplier.getFw_tc() != null)
            aa.append(supplier.getFw_tc());//如果父节点存在，加入父节点的cate_code
        aa.append(MathUtil3.math3());//末尾加入3位编码
        supplier.setCate_code(aa.toString());
        System.out.println(supplier.getCate_code());
        int a = bdIndustryChartEntityDao.insertSelective(supplier);
        if (a > 0) {
            rsDto.setStatus(0);
            rsDto.setMessage("添加成功！");
            rsDto.setData(supplier);
        }else  {
            rsDto.setStatus(1);
            rsDto.setMessage("添加失败！");
        }
        return rsDto;
	}

	public ResultDto selectByPrimaryKey(String paramString) {
		ResultDto rsDto = new ResultDto();
		BdIndustryChartEntity listvo = bdIndustryChartEntityDao.selectByPrimaryKey(paramString);
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setData(listvo);
		return rsDto;
		
	}

	public ResultDto selectAll() {
		ResultDto rsDto = new ResultDto();
		List<BdIndustryChartEntity> listvo = bdIndustryChartEntityDao.selectAll();
		rsDto.setStatus(0);
		rsDto.setMessage("数据查询成功!");
		rsDto.setData(listvo);
		rsDto.setNum(listvo==null?0:listvo.size());
		rsDto.setCount(listvo==null?0:listvo.size());
		return rsDto;
	}

	public ResultDto updateByPrimaryKey(BdIndustryChartEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			int i_del = bdIndustryChartEntityDao.updateByPrimaryKey(supplier);
			rsDto.setStatus(0);
			rsDto.setMessage("修改完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		
		return rsDto;
	}

	public ResultDto selectByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			 List<BdIndustryChartEntity>  listvo= bdIndustryChartEntityDao.selectByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setData(listvo);
			rsDto.setNum(listvo==null?0:listvo.size());
			rsDto.setCount(listvo==null?0:listvo.size());
		}
		return rsDto;
	}

	public ResultDto countByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			 int i_del = bdIndustryChartEntityDao.countByCondition(paramString);

			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
			rsDto.setNum(i_del);
		}
		return rsDto;
	}

	public ResultDto deleteByCondition(String paramString) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(paramString)){
			rsDto.setStatus(1);
			rsDto.setMessage("删除条件不能为空!");
		}else{
			int i_del = bdIndustryChartEntityDao.deleteByCondition(paramString);
			rsDto.setStatus(0);
			rsDto.setMessage("删除完成!");
		}
		
		return rsDto;
	}

	public ResultDto insertSelective(BdIndustryChartEntity supplier) {
			ResultDto rsDto = new ResultDto();
		
		String code=MathUtil3.math3();
		supplier.setPk_industry_chart(MathUtil3.uuid32());
		StringBuffer aa=new StringBuffer();
		if(supplier!=null&&supplier.getFw_tc()!=null){
			aa.append(supplier.getFw_tc());
			aa.append(code);
			supplier.setFw_tc(aa.toString());
			int a=  bdIndustryChartEntityDao.insertSelective(supplier);
			if(a>0){
				rsDto.setStatus(0);
				rsDto.setMessage("添加成功！");
			}
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("添加失败！");
		}
		
		 return rsDto;
	}

	public ResultDto updateByPrimaryKeySelective(BdIndustryChartEntity supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			int i_del = bdIndustryChartEntityDao.updateByPrimaryKeySelective(supplier);
			rsDto.setStatus(0);
			rsDto.setMessage("修改完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("修改失败!");
		}
		
		return rsDto;
	}

	public ResultDto queryHyGxSb(Map<String, Object> supplier) {
		ResultDto rsDto = new ResultDto();
		if(supplier!=null){
			List<BdHyGxSbBasePoJo> i_del = bdIndustryChartEntityDao.queryHyGxSb(supplier);
			
			rsDto.setStatus(0);
			rsDto.setData(getHyGxSbList(i_del));
			rsDto.setMessage("查询完成!");
		}else{
			rsDto.setStatus(1);
			rsDto.setMessage("查询失败!");
		}
		
		return rsDto;
	}
	/**
	 * 构建JSON对象
	 * @param i_del
	 */
	private List<BdHyGxSbPoJo> getHyGxSbList(List<BdHyGxSbBasePoJo> i_del){
		if(i_del==null || i_del.size()==0){
			return null;
		}
		List<BdHyGxSbPoJo> hyMap = new ArrayList<BdHyGxSbPoJo>();
		List<BdHyGxSbPoJo> gxMap = new ArrayList<BdHyGxSbPoJo>();
		List<BdHyGxSbPoJo> sbMap = new ArrayList<BdHyGxSbPoJo>();
	//		去重复
		List<String> temp=new ArrayList<String>();
		for(BdHyGxSbBasePoJo bean:i_del){
	//		行业
			if(!temp.contains(bean.getPk_hy())){
				BdHyGxSbPoJo data = new BdHyGxSbPoJo();
				data.setCate_code(bean.getHy_code());
				data.setCate_name(bean.getHy_name());
				data.setSelected(bean.getIs_selected());
				data.setPk_industry_chart(bean.getPk_hy());
				data.setPk_parent(bean.getHy_parent());
				hyMap.add(data);
				temp.add(bean.getPk_hy());
			}
			
			//工序
			if(!temp.contains(bean.getPk_gx())){
				BdHyGxSbPoJo gx = new BdHyGxSbPoJo();
				gx.setCate_code(bean.getGx_code());
				gx.setCate_name(bean.getGx_name());
				gx.setSelected(bean.getIs_selected());
				gx.setPk_industry_chart(bean.getPk_gx());
				gx.setPk_parent(bean.getGx_parent());
				gxMap.add(gx);
				temp.add(bean.getPk_gx());
			}
			//设备
			if(!temp.contains(bean.getPk_sb())){
				BdHyGxSbPoJo sb = new BdHyGxSbPoJo();
				sb.setCate_code(bean.getSb_code());
				sb.setCate_name(bean.getSb_name());
				sb.setSelected(bean.getIs_selected());
				sb.setPk_industry_chart(bean.getPk_sb());
				sb.setPk_parent(bean.getSb_parent());
				sbMap.add(sb);
				temp.add(bean.getPk_sb());
			}
		}
		
		for(BdHyGxSbPoJo hy : hyMap){
			
			List<BdHyGxSbPoJo> gxlist = new ArrayList<BdHyGxSbPoJo>();
			for(BdHyGxSbPoJo gx : gxMap){
				if(hy.getPk_industry_chart().equals(gx.getPk_parent())){
					gxlist.add(gx);
					List<BdHyGxSbPoJo> sblist = new ArrayList<BdHyGxSbPoJo>();
					for(BdHyGxSbPoJo sb : sbMap){
						if(gx.getPk_industry_chart().equals(sb.getPk_parent())){
							sblist.add(sb);
						}
					}
					gx.setList(sblist);
				}
			}
			hy.setList(gxlist);
		}
		return hyMap;
	}
	/* 
	 * param 查询参数
	 * cate_type 从哪一级开始
	 * flae “0”多级行业“1”只查两级
	 */
	public ResultDto queryTree(String param,int cate_type,String flae) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(param)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<BdIndustryTreePoJo> i_del = bdIndustryChartEntityDao.queryTree(param);
			if("0".equals(flae)){
				rsDto.setData(i_del);
				rsDto.setStatus(0);
				rsDto.setMessage("查询成功!");
			}if("1".equals(flae)){
				List<BdIndustryTreePoJo> flist=new ArrayList<BdIndustryTreePoJo>();
				List<BdIndustryTreePoJo> slist=new ArrayList<BdIndustryTreePoJo>();
				for(BdIndustryTreePoJo bean:i_del){
					if(bean.getCate_type()==cate_type){
						flist.add(bean);
					}else if(bean.getCate_type()==cate_type+1){
						slist.add(bean);
					}
				}
				for(BdIndustryTreePoJo fbean:flist){
					List<BdIndustryTreePoJo> temp=new ArrayList<BdIndustryTreePoJo>();
					for(BdIndustryTreePoJo sbean:slist){
						if(fbean.getPk_industry_chart().equals(sbean.getFw_parent())){
							temp.add(sbean);
						}
					}
					fbean.setData(temp);
				}
				rsDto.setData(flist);
				rsDto.setStatus(0);
				rsDto.setMessage("查询成功!");
			}
		}
		return rsDto;
	}

	/* 
	 * 查询三四级关键字
	 */
	public ResultDto queryLike(String param) {
		ResultDto rsDto = new ResultDto();
		if(StringUtils.isEmpty(param)){
			rsDto.setStatus(1);
			rsDto.setMessage("查询条件不能为空!");
		}else{
			List<BdIndustryTreePoJo> i_del = bdIndustryChartEntityDao.queryLike(param);
			List<BdIndustryTreePoJo> flist=new ArrayList<BdIndustryTreePoJo>();
			List<BdIndustryTreePoJo> slist=new ArrayList<BdIndustryTreePoJo>();
			for(BdIndustryTreePoJo bean:i_del){
				
				if(bean.getCate_type()==2){
					//第三级关键字
					flist.add(bean);
				}else if(bean.getCate_type()==3){
					//第四级关键字
					slist.add(bean);
				}
			}
			for(BdIndustryTreePoJo fbean:flist){
				List<BdIndustryTreePoJo> temp=new ArrayList<BdIndustryTreePoJo>();
				for(BdIndustryTreePoJo sbean:slist){
					if(fbean.getPk_industry_chart().equals(sbean.getFw_parent())){
						temp.add(sbean);
					}
				}
				fbean.setData(temp);
			}
			rsDto.setData(flist);
			rsDto.setStatus(0);
			rsDto.setMessage("查询成功!");
		}
		return rsDto;
	}
	
	public ResultDto queryAll(Map map) {
		ResultDto rsDto = new ResultDto();
		List<BdIndustryTreePoJo> i_del = bdIndustryChartEntityDao.queyrAll(map);

		rsDto.setStatus(0);
		rsDto.setMessage("查询成功!");
		rsDto.setData(i_del);
		return rsDto;
	}

	public ResultDto queryHyGxSbByUserSet(Map map) {
		ResultDto rsDto = new ResultDto();
		if(map!=null && map.get("pk_user")!=null){
			List<BdHyGxSbBasePoJo> i_del = bdIndustryChartEntityDao.queryHyGxSbByUserSet(map);
			rsDto.setStatus(0);
			rsDto.setData(getHyGxSbList(i_del));
			rsDto.setMessage("查询完成!");
		}else{
			rsDto.setStatus(-1);
			rsDto.setMessage("缺少必要的传入参数pk_user!");
		}
		
		return rsDto;
	}

	public ResultDto queryHyGxSbByHyPK(Map map) {
		ResultDto rsDto = new ResultDto();
		if(map!=null && map.get("cate_name")!=null){
			List<BdHyGxSbBasePoJo> i_del = bdIndustryChartEntityDao.queryHyGxSbByHyPK(map);
			rsDto.setStatus(0);
			rsDto.setData(getHyGxSbList(i_del));
			rsDto.setMessage("查询完成!");
		}else{
			rsDto.setStatus(-1);
			rsDto.setMessage("缺少必要的传入参数cate_name!");
		}
		
		return rsDto;
	}

}
